package com.bsoft.exchange.service;

import com.bsoft.exchange.dao.his.HospitalPatientMapper;
import com.bsoft.exchange.domain.*;
import com.bsoft.exchange.pojo.GetPatientListPojo;
import com.bsoft.exchange.pojo.ListAllAdvicePojo;
import com.bsoft.exchange.util.BaseResponse;
import com.bsoft.exchange.util.Constant;
import com.bsoft.exchange.util.SumPriceUtil;
import com.bsoft.exchange.util.TextUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建日期 : 2019/10/23
 * 创建人 : 赵太行
 */
@Service
public class HospitalPatientService {
    @Autowired
    HospitalPatientMapper hospitalPatientMapper;
    int pageNo;
    int pageSize;
    private String cfhm;
    @Autowired
    PublicService publicService;

    /**
     * 获取医生对应科室
     */
    public BaseResponse getDocDepartment(String jgid,String ysdm){
        BaseResponse baseResponse=new BaseResponse();
        List<DepartmentDomain> departmentDomainList=hospitalPatientMapper.getDocDepartment(jgid, ysdm);
        if (departmentDomainList.size()==0){
            baseResponse.setMessage("为查询到该医生对应的科室信息");
        }else{
            baseResponse.setMessage("查询成功");
        }
        baseResponse.setCode(Constant.successResponse);
        baseResponse.setData(departmentDomainList);
        return baseResponse;
    }
    /**
     * 获取医生管理的患者列表
     */
    public BaseResponse getPatientList(GetPatientListPojo pojo){
        BaseResponse baseResponse=new BaseResponse();
        List<Integer>zyhList=new ArrayList<>();
        if (pojo.getPageNo()==null) {
            pageNo = 1;
        }else{
            pageNo=pojo.getPageNo();
        }
        if (pojo.getPageSize()==null) {
            pageSize = 10;
        }else{
            pageSize=pojo.getPageSize();
        }

        if (!TextUtils.isEmpty(pojo.getQuery())) {
            cfhm = "%" + pojo.getQuery() + "%";
        }
        String ysdm=pojo.getDoctorId();
        String ksdm=pojo.getDepartmentId();
        if (!TextUtils.isEmpty(ysdm)&&!TextUtils.isEmpty(ksdm)){
            ksdm=null;
        }
        PageHelper.startPage(pageNo, pageSize);
        List<GetPatientDomain>patientList=hospitalPatientMapper.getPatientList(pojo.getOrganizationId(),ysdm,ksdm,cfhm);
        if (patientList.size()==0){
            baseResponse.setCode(Constant.successResponse);
            baseResponse.setMessage("未查询到病人列表");
            return baseResponse;
        }
        //获取员工信息
        List<DoctorInfo>doctorInfo=publicService.getDoctorInfo(pojo.getOrganizationId());
        for (GetPatientDomain getPatientDomain:patientList){
            for (DoctorInfo doctor:doctorInfo){
                if (getPatientDomain.getZZYS().equals(doctor.getYGDM())){
                    getPatientDomain.setPrincipalDoctor(doctor.getYGXM());
                }
                if (!TextUtils.isEmpty(getPatientDomain.getZRHS())) {
                    if (getPatientDomain.getZRHS().equals(doctor.getYGDM())) {
                        getPatientDomain.setResponsibleNurse(doctor.getYGXM());
                    }
                }
            }
            zyhList.add(getPatientDomain.getInHospitalRecordNumber());
        }
        //获取住院费用
        List<FymxMoney> feeList=hospitalPatientMapper.getPrecalculatedFee(pojo.getOrganizationId(),zyhList);
        List<FymxMoney> tbkkList=hospitalPatientMapper.getTbkk(pojo.getOrganizationId(),zyhList);
        for (GetPatientDomain getPatientDomain:patientList){
            Double zfze=0.00;
            Double yjze = 0.00;
            for (FymxMoney fymxMoney:feeList){
                if (getPatientDomain.getInHospitalRecordNumber().equals(fymxMoney.getZYH())){
                    zfze=fymxMoney.getPayAmount();
                    break;
                }
            }
            for (FymxMoney fymxMoney:tbkkList){
                if (getPatientDomain.getInHospitalRecordNumber().equals(fymxMoney.getZYH())){
                    yjze=fymxMoney.getYJJE();
                    break;
                }
            }
            //判断是否欠费=押金-自费金额
            Double qfje=SumPriceUtil.getMinusPriceDouble(zfze,yjze);
            if (qfje>=0){
                //表示不欠费
                getPatientDomain.setIsArrears(1);
                getPatientDomain.setAmountArrears(qfje);
            }else{
                getPatientDomain.setIsArrears(0);
            }
            if (TextUtils.isEmpty(getPatientDomain.getBRQK())){
                getPatientDomain.setIsCrisis(0);
            }else{
                if (getPatientDomain.getBRQK().equals("3")||getPatientDomain.getBRQK().equals("4")){
                    getPatientDomain.setIsCrisis(0);
                }else{
                    getPatientDomain.setIsCrisis(1);
                }
            }
            if (TextUtils.isEmpty(getPatientDomain.getAllergyCondition())){
                getPatientDomain.setIsAllergy(0);
            }else{
                getPatientDomain.setIsAllergy(1);
            }
            if (getPatientDomain.getBRXB()==1){
                getPatientDomain.setPatientSex("男");
            }else{
                getPatientDomain.setPatientSex("女");
            }
        }
        baseResponse.setData(patientList);
        baseResponse.setCode(Constant.successResponse);
        baseResponse.setMessage("请求成功");
        return baseResponse;
    }

    /**
     * 获取住院医嘱
     */
    public BaseResponse listAllAdvice(ListAllAdvicePojo pojo){
        BaseResponse <List<ListAllAdviceDomain>> baseResponse=new BaseResponse();

        Long jgid;
        Long zyh;
        if(pojo.getOrganizationId() == null || "".equals(pojo.getOrganizationId())){
            baseResponse.setCode(Constant.errorTag);
            baseResponse.setMessage("医院识别代码不能为空!");

            return baseResponse;
        }
        if(pojo.getInHospitalRecordNumber() == null || "".equals(pojo.getInHospitalRecordNumber())){
            baseResponse.setCode(Constant.errorTag);
            baseResponse.setMessage("住院号不能为空!");

            return baseResponse;
        }
        jgid = Long.parseLong(pojo.getOrganizationId());
        zyh = Long.parseLong(pojo.getInHospitalRecordNumber());

        Integer type;
        if(pojo.getType() == null || "".equals(pojo.getType())){
            type = 1;
        }else{
            type = pojo.getType();
        }

        //分页
        PageHelper.startPage(pojo.getPageNo(),pojo.getPageSize());
        List<ListAllAdviceDomain> adviceList=hospitalPatientMapper.getAllAdvice(jgid,zyh,type);

        if (adviceList.size()==0){
            baseResponse.setMessage("未查询到医嘱信息");
        }else{
            baseResponse.setMessage("查询成功");
        }
        baseResponse.setCode(Constant.successResponse);
        baseResponse.setData(adviceList);
        return baseResponse;
    }

}
